package com.dingqb.mycs;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Point;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.Display;
import android.view.Window;
import android.widget.TextView;

import com.dingqb.mycs.R;
import com.dingqb.mycs.ui.entity.Dimension;
import com.dingqb.util.ScreenWH;

/**
 * http://blog.csdn.net/xinhai657/article/details/16863679
 * 查看屏幕宽高
 * @author DingQingbo
 *
 */
public class ScreenSizeActivity extends Activity {
	private TextView mScreenSizeView;
	ViewPager v;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_screen_size);
		mScreenSizeView = (TextView) findViewById(R.id.screen_size);
	}

	@Override
	public void onWindowFocusChanged(boolean hasFocus) {
		super.onWindowFocusChanged(hasFocus);
		if (hasFocus) {
			System.out.println("second");
			StringBuilder sb = new StringBuilder();
			Dimension dimen1 = getAreaOne(this);
			Dimension dimen2 = getAreaTwo(this);
			Dimension dimen3 = getAreaThree(this);
			sb.append("Area one : \n\tWidth: " + dimen1.mWidth + ";\tHeight: "
					+ dimen1.mHeight);
			sb.append("\nArea two: \n\tWidth: " + dimen2.mWidth + ";\tHeight: "
					+ dimen2.mHeight);
			sb.append("\nArea three: \n\tWidth: " + dimen3.mWidth
					+ ";\tHeight: " + dimen3.mHeight);
			sb.append("\np.x + \"*\" + p.y" + getResolution((this)));
			sb.append("\nDisplayMetrics dm = new DisplayMetrics();context.getWindowManager().getDefaultDisplay().getMetrics(dm);"
					+ ScreenWH.getScreenHeight(this));
			sb.append("\nDisplayMetrics dm2 = context.getResources().getDisplayMetrics();context.getWindowManager().getDefaultDisplay().getMetrics(dm2);"
					+ ScreenWH.getScreenHeight2(this));
			sb.append("\nDisplay display = context.getWindowManager().getDefaultDisplay();"
					+ ScreenWH.getScreenHeight3(this));
			sb.append("\n获取屏幕密度" + ScreenWH.getScreenDensity(this));
			sb.append("\n获取屏幕密度" + ScreenWH.getScreenDPI(this));
			sb.append("\n状态栏高度" + ScreenWH.getStatusHeight(this));
			mScreenSizeView.setText(sb.toString());
		}
	}

	/**
	 * 包括状态栏
	 * 
	 * @param activity
	 * @return
	 */
	@SuppressLint("NewApi")
	private Dimension getAreaOne(Activity activity) {
		Dimension dimen = new Dimension();
		Display disp = activity.getWindowManager().getDefaultDisplay();
		Point outP = new Point();
		disp.getSize(outP);
		dimen.mWidth = outP.x;
		dimen.mHeight = outP.y;
		return dimen;
	}

	/**
	 * 去除状态栏,
	 * 
	 * @param activity
	 * @return
	 */
	private Dimension getAreaTwo(Activity activity) {
		Dimension dimen = new Dimension();
		Rect outRect = new Rect();
		activity.getWindow().getDecorView()
				.getWindowVisibleDisplayFrame(outRect);
		System.out.println("top:" + outRect.top + " ; left: " + outRect.left);
		dimen.mWidth = outRect.width();
		dimen.mHeight = outRect.height();
		return dimen;
	}

	/**
	 * 去除状态栏，去除标题栏（NoActionBar）
	 * 
	 * @param activity
	 * @return
	 */
	private Dimension getAreaThree(Activity activity) {
		Dimension dimen = new Dimension();
		// 用户绘制区域
		Rect outRect = new Rect();
		activity.getWindow().findViewById(Window.ID_ANDROID_CONTENT)
				.getDrawingRect(outRect);
		dimen.mWidth = outRect.width();
		dimen.mHeight = outRect.height();
		// end
		return dimen;
	}

	/**
	 * 获取分辨率
	 * 
	 * @param context
	 * @return
	 */

	@SuppressLint("NewApi")
	public static String getResolution(Activity context) {
		Display dis = context.getWindowManager().getDefaultDisplay();
		Point p = new Point();
		dis.getSize(p);
		return p.x + "*" + p.y;
	}

}